//
//  GroupQRCodeViewController.m
//  AAIM
//
//  Created by awh on 2019/12/3.
//  Copyright © 2019年 AAIM. All rights reserved.
//

#import "GroupQRCodeViewController.h"

@interface GroupQRCodeViewController ()
@property(nonatomic,strong)UIView * backView;
@property(nonatomic,strong)UIImageView * headimage;
@property(nonatomic,strong)UILabel * namelbl;
@property(nonatomic,strong)UIImageView * QRCodeImage;
@property(nonatomic,strong)UILabel * titlelbl;
@property(nonatomic,strong)UIButton*saveButton;
@end

@implementation GroupQRCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.gk_navTitle=@"群二维码";
    self.view.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1.0];

    [self.view addSubview:self.backView];
    [self.view addSubview:self.headimage];
    [self.backView addSubview:self.namelbl];

    _QRCodeImage=[[UIImageView alloc]initWithFrame:CGRectMake(55, _namelbl.bottom+35,_backView.width-110, _backView.width-110)];
    [_QRCodeImage setContentScaleFactor:[[UIScreen mainScreen] scale]];
    _QRCodeImage.contentMode =  UIViewContentModeScaleAspectFit;
//    _QRCodeImage.image=[self createNonInterpolatedUIImageFormCIImage:[self creatQRcodeWithUrlstring:_QRUrl] withSize:_QRCodeImage.width];
    NSData * groupCodeData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",userInfoImage,_QRUrl]]];
    _QRCodeImage.image = [UIImage imageWithData:groupCodeData];
    [self.backView addSubview:self.QRCodeImage];

    _headimage.image = _groupHeadImage;
//    [self.backView addSubview:self.titlelbl];

    // Do any additional setup after loading the view.
}
-(UIView* )backView
{
    if (!_backView) {
        _backView=[[UIView alloc]initWithFrame:CGRectMake(30, NavigationBarHeight+50, SCREEN_WIDTH-60, 450)];
        _backView.backgroundColor=[UIColor whiteColor];
        _backView.layer.masksToBounds=YES;
        _backView.layer.cornerRadius=5;
    }
    return _backView;
}
-(UIImageView*)headimage
{
    if (!_headimage) {
        _headimage=[[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-35, NavigationBarHeight+20, 70, 70)];
//        _headimage.backgroundColor=[UIColor redColor];
        _headimage.layer.masksToBounds=YES;
        _headimage.layer.cornerRadius=35;
            [_headimage setContentScaleFactor:[[UIScreen mainScreen] scale]];
        _headimage.contentMode =  UIViewContentModeScaleAspectFit;

        _headimage.image = _groupHeadImage;
       
    }
    return _headimage;
}
-(UILabel*)namelbl
{
    if (!_namelbl) {
        _namelbl=[self CreateLabelWithFrame:CGRectMake(10, 80, _backView.width-20, 20) Font:18 titleColor:KHEXCOLOR(@"#262B2F")];
        _namelbl.text=_groupName;
    }
    return _namelbl;
}
-(UILabel*)titlelbl
{
    if (!_titlelbl) {
        _titlelbl=[self CreateLabelWithFrame:CGRectMake(10, _QRCodeImage.bottom+10, _backView.width-20, 20) Font:12 titleColor:KHEXCOLOR(@"#262B2F")];
        _titlelbl.text=@"扫一扫，加入群聊";
    }
    return _titlelbl;
}
-(UIButton*)saveButton
{
    if (_saveButton) {
        _saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_saveButton setTitle:@"保存" forState:UIControlStateNormal];
        [_saveButton setImage:[UIImage imageNamed:@"保存按钮"] forState:UIControlStateNormal];
        [_saveButton setTitleEdgeInsets:UIEdgeInsetsMake(15, -18, -15, 18)];
        [_saveButton setImageEdgeInsets:UIEdgeInsetsMake(-15, 15, 15, -15)];
        [_saveButton addTarget:self action:@selector(saveImgAction) forControlEvents:UIControlEventTouchUpInside];
        [_saveButton setTitleColor:KHEXCOLOR(@"#AFAFAF") forState:UIControlStateNormal];
        _saveButton.titleLabel.font = kMainFont(15);
    }
    return _saveButton;
}
-(void)saveImgAction
{
    [self showLoadingHud];
    UIImageWriteToSavedPhotosAlbum(self.QRCodeImage.image, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
  
}
- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    [self hideHud];
    if (!error) {
        [self showTipHudWithText:@"保存成功"];
    }else
    {
        [self showTipHudWithText:@"保存失败"];
    }
}
-(CGRect)getWaterImageRectFromOutputQRImage:(UIImage *)orginQRImage{
    
    CGSize linkSize = CGSizeMake(orginQRImage.size.width / 5, orginQRImage.size.height / 5);
    
    CGFloat linkX = (orginQRImage.size.width -linkSize.width)/2;
    CGFloat linkY = (orginQRImage.size.height -linkSize.height)/2;
    
    return CGRectMake(linkX, linkY, linkSize.width, linkSize.height);
}
-(UIImage*)scaleToSize:(CGSize)size
{
    size = CGSizeMake(size.width  , size.height );
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContext(size);
    // 绘制改变大小的图片
    [_QRCodeImage.image drawInRect:CGRectMake(0, 0, size.width , size.height )];
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    // 返回新的改变大小后的图片
    return scaledImage;
}

- (CIImage *)creatQRcodeWithUrlstring:(NSString *)urlString{
    
    // 1.实例化二维码滤镜
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    // 2.恢复滤镜的默认属性 (因为滤镜有可能保存上一次的属性)
    [filter setDefaults];
    // 3.将字符串转换成NSdata
    NSData *data  = [urlString dataUsingEncoding:NSUTF8StringEncoding];
    // 4.通过KVO设置滤镜, 传入data, 将来滤镜就知道要通过传入的数据生成二维码
    [filter setValue:data forKey:@"inputMessage"];
    // 5.生成二维码
    CIImage *outputImage = [filter outputImage];
    return outputImage;
}
- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size
{
    CGRect extent = CGRectIntegral(image.extent);
    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
    
    // 1.创建bitmap;
    size_t width = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    
    // 2.保存bitmap到图片
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    return [UIImage imageWithCGImage:scaledImage];
}
-(UILabel*)CreateLabelWithFrame:(CGRect)Frame Font:(CGFloat)font titleColor:(UIColor*)titleColor
{
    UILabel * lbl=[[UILabel alloc]initWithFrame:Frame];
    lbl.font=[UIFont systemFontOfSize:font];
    lbl.textColor=titleColor;
    lbl.textAlignment=1;
    return lbl;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
